﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;
using QAHelper.Common;

namespace Sentrana_QA.WatiNScript.SpoonByteTestScript
{
    public static class RegistrationStepOne
    {
        public static string TestBRSS_B2_RR1_1(Browser window, DictionaryEntry[] tempTestData)
        {
            #region Local Parameters
            string result = string.Empty;
            string invalidText = "Wrong restaurant Id";
            ArrayList testData = new ArrayList();
            #endregion

            #region Validating URLs

            string url = null;

            for (int i = 0; i < tempTestData.Length; i++)
            {
                testData = CommonHelper.GetDataFromDictionary(tempTestData, i);

                foreach (DictionaryEntry urlDict in testData)
                {
                    url = urlDict.Value.ToString();
                    try
                    {
                        window.GoTo(url);
                        window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));

                        result = (result == null) ? result = url + ": " : result += Environment.NewLine+Environment.NewLine + url + ": ";

                        if (window.Text.Contains(invalidText))
                            result += urlDict.Key.ToString()+Environment.NewLine+Environment.NewLine+invalidText;
                        else if (window.Text.Contains(StepHeaderText.Step1HeaderMessage))
                            result += urlDict.Key.ToString()+Environment.NewLine+"Valid restaurant ID and Restaurant Registration page loaded successfully.";
                        else if (window.Text.Contains(StepHeaderText.LoginHeaderMessage))
                            result += urlDict.Key.ToString() + Environment.NewLine + "Given restaurant ID already registered.";
                        else
                            result += urlDict.Key.ToString() + QAHelper.Common.Enum.StatusText.Fail.ToString()+ Environment.NewLine + "Unexpected Error";
                    }

                    catch (Exception e)
                    {
                        result = (result == null) ? result = urlDict.Key.ToString() + QAHelper.Common.Enum.StatusText.Fail.ToString() + url + ": " + e.ToString() : result += urlDict.Key.ToString() + QAHelper.Common.Enum.StatusText.Fail.ToString() + Environment.NewLine + Environment.NewLine + url + ": " + e.ToString();
                    }
                    
                }
            }
            #endregion

            return result;
        }

        public static string TestBRSS_B2_RR1_2(Browser window, DictionaryEntry[] tempTestData)
        {
            #region Local Parameters
            string result = string.Empty;
            //string validText = "Step 1 of 6: Enter Restaurant Operator Information";
            string URL = "http://192.168.1.102/restaurantweb/RestaurantAdmin/register?restaurantId=128";
            string syscoID = null;
            Image captchaImage = null;
            int index = URL.LastIndexOf('=');
            #endregion      
            
            #region Validating Page
            try
            {
                window.GoTo(URL);
                window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));

                result = (string.IsNullOrEmpty(result)) ? result = URL + ": " : result += Environment.NewLine + Environment.NewLine + URL + ": ";

                if (window.Text.Contains(StepHeaderText.Step1HeaderMessage))
                {
                    captchaImage = window.Image(Find.ById("CaptchaImageDiv"));

                    if (captchaImage != null && window.TextField(Find.ByName("BussinessName")).Text.Trim() != null
                        && window.ContainsText(syscoID))
                    {
                        result += Environment.NewLine+"Registration page loaded successfully and the following are present in the page: Business name, Sysco ID & Captcha image.";
                    }
                    else
                    {
                        result += Environment.NewLine+"Registration page did not load successfully.";
                    }
                }
                else
                    result += Environment.NewLine+"Unexpected Error";
            }
            catch (Exception e)
            {
                result = (result == null) ? result = URL + ": ERROR - " + e.ToString() : result += Environment.NewLine+Environment.NewLine + URL + ": ERROR - " + e.ToString();
            }

            #endregion                 

            return result;
        }

        public static string TestBRSS_B2_RR1_3(Browser window, DictionaryEntry[] tempTestData)
        {
            #region Local Parameters

            string URL = Properties.Settings.Default.NEW_RESTAURANT_REGISTRATION_URL;
            string result = string.Empty;

            #endregion

            if (tempTestData.Length > 1)
            {
                #region Test Runs

                #region Set-1 (Validate Empty Mandatory Fields)

                result = EmptyMandatoryField(window, URL, tempTestData);

                #endregion

                #region Set-2 (Invalid email format)

                result += Environment.NewLine + InvalidEmailFormat(window, URL, tempTestData);

                #endregion

                #region Set-3 (Email Mismatch)

                result += Environment.NewLine + EmailsMismatch(window, URL, tempTestData);

                #endregion

                #region Set-4 (Invalid password length)

                result += Environment.NewLine + "Yet to be implemented in Spoonbyte.";
                //InvalidPasswordLength(URL, tempTestData); /* PASSWORD LENGTH CHECKING IS NOT IMPLEMENTED YET */

                #endregion

                #region Set-5 (Password mismatch)

                result += Environment.NewLine + PasswordsMismatch(window, URL, tempTestData);

                #endregion
            }

            #region Set-6 (Valid Entries)

            result += Environment.NewLine+ValidEntries(window, URL, tempTestData);

            #endregion

            #endregion

            return result;
        }

        public static string TestBRSS_B2_RR1_4(Browser window, DictionaryEntry[] tempTestData)
        {
            #region Local Parameters

            string result = null;
            string URL = "http://192.168.1.102/restaurantweb/RestaurantAdmin/register?restaurantId=128";
            string filename = "C:\\SpoonByte_Auto_Script.txt";
            int fields = 0;

            DictionaryEntry[] values = new DictionaryEntry[6];
            values[0] = new DictionaryEntry("FirstName", "FName");
            values[1] = new DictionaryEntry("LastName", "LName");
            values[2] = new DictionaryEntry("EmailAddress", "my@email.com");
            values[3] = new DictionaryEntry("ReEmailAddress", "my@email.com");
            values[4] = new DictionaryEntry("Password", "123456");
            values[5] = new DictionaryEntry("ConfirmPassword", "123456");

            Link link = null;

            #endregion            

            #region Validating Page
            try
            {
                link = window.Link(Find.ByText("Try another image"));
                result = (result == null) ? result = "URL: " + URL : result += Environment.NewLine+Environment.NewLine + "URL: " + URL;

                #region Empty form
                window.GoTo(URL);
                window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));
                link.Click();
                if (window.ContainsText(StepHeaderText.Step1HeaderMessage))
                    result += Environment.NewLine+Environment.NewLine + "-> CAPTCHA image reloaded successfully for empty form." + Environment.NewLine;
                else
                    result += Environment.NewLine+Environment.NewLine + "-> CAPTCHA image do not reloaded for empty form." + Environment.NewLine;
                #endregion

                #region Completed form
                window.GoTo(URL);
                window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));
                WatiNInputHelper.SetPageInput(window, values, out fields);

                link = window.Link(Find.ByText("Try another image"));
                link.Click();
                if (window.ContainsText(StepHeaderText.Step1HeaderMessage))
                {
                    string captchaValue = (window.TextField(Find.ByName("CaptchaText")).Text != null) ? captchaValue = window.TextField(Find.ByName("CaptchaText")).Text.Trim()
                        : captchaValue = window.TextField(Find.ByName("CaptchaText")).Text;

                    //Checking if all fields retain their value or not. Exception for CAPTCHA field which should not retain the value
                    if (window.TextField(Find.ByName("FirstName")).Text.Trim().Length > 0 && window.TextField(Find.ByName("LastName")).Text.Trim().Length > 0
                        && window.TextField(Find.ByName("EmailAddress")).Text.Trim().Length > 0 && window.TextField(Find.ByName("ReEmailAddress")).Text.Trim().Length > 0
                        && window.TextField(Find.ByName("Password")).Text.Trim().Length > 0 && window.TextField(Find.ByName("ConfirmPassword")).Text.Trim().Length > 0
                        && captchaValue == null)
                    {
                        result += Environment.NewLine+"-> CAPTCHA image reloaded successfully for filled-up form, fields retain their values and the CAPTCHA text field is cleared on image reload." + Environment.NewLine;
                    }
                    //if field value(s) not retained
                    else
                        result += Environment.NewLine+"-> CAPTCHA image reloaded but field value(s) are not retained on page reload." + Environment.NewLine;
                }
                else
                    result += Environment.NewLine+"-> CAPTCHA image do not reloaded for filled-up form." + Environment.NewLine;
                #endregion
            }
            catch (Exception e)
            {
                result = (result == null) ? result = URL + ": ERROR - " + e.ToString() : result += Environment.NewLine+Environment.NewLine + URL + ": ERROR - " + e.ToString();
            }
            #endregion

            return result;
        }

        public static string TestBRSS_B2_RR1_5(Browser window, DictionaryEntry[] tempTestData)
        {
            #region Local Parameters
            string verificationText = "EmailAddress";
            string result = null;
            Button cancel;
            Button submit;
            int noOfFields = 0;
            #endregion

            #region Cancel button action for empty field

            window.GoTo(Properties.Settings.Default.NEW_RESTAURANT_REGISTRATION_URL);
            window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));
            cancel = window.Button(Find.ByValue(ButtonsName.CancelButton));
            cancel.Click();
            window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));

            if (window.TextField(Find.ById(verificationText)).Exists)
                result = "Cancel button for empty form works properly." + Environment.NewLine;
            else
                result = "Cancel button for empty form does not workS properly." + Environment.NewLine;

            #endregion

            #region Cancel button action for completed form

            window.GoTo(Properties.Settings.Default.NEW_RESTAURANT_REGISTRATION_URL);
            window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));

            WatiNInputHelper.SetPageInput(window, tempTestData, out noOfFields);

            cancel = window.Button(Find.ByValue(ButtonsName.CancelButton));
            cancel.Click();
            window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));

            if (window.TextField(Find.ById(verificationText)).Exists)
                result += Environment.NewLine+"Cancel button for completed form works properly." + Environment.NewLine;
            else
                result += Environment.NewLine+"Cancel button for completed form does not workS properly." + Environment.NewLine;

            #endregion

            #region Submit button action for completed form

            result += Environment.NewLine+Save1stStep(window, tempTestData);        

            #endregion

            return result;
        }


        #region Private methods

        #region BRSS_B2_RR1_3 TEST RUNS

        #region Empty Mandatory Field Test BRSS_B2_RR1_3
        /* 2010-06-17: FIXED */
        private static string EmptyMandatoryField(Browser window, string URL, DictionaryEntry[] testData)
        {
            #region Local Parameters
            DictionaryEntry[] emptyValues = new DictionaryEntry[7];
            Button saveButton = null;
            string result = string.Empty;
            int fields = 0;
            #endregion

            #region Empty mandatory fields
            try
            {
                window.GoTo(URL);
                window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));
                result = Properties.Settings.Default.DATA_SET_DELIM_START + testData[0].Key.ToString()+ Properties.Settings.Default.DATA_SET_DELIM_END + Environment.NewLine;

                if (!window.ContainsText(StepHeaderText.Step1HeaderMessage))
                {
                    result += Environment.NewLine+"Given restaurant already registered";
                    return string.Empty;
                }

                saveButton = window.Button(Find.ByValue(ButtonsName.SaveButton));

                ArrayList thisStepArrayList = CommonHelper.GetDataFromDictionary(testData, 0);

                WatiNInputHelper.SetPageInput(window, thisStepArrayList, out fields);
                saveButton.Click();

                if (window.ContainsText(MessageInventory.ErrorMessageHeaderText))
                {
                    //First name
                    if (!window.ContainsText(Step1Errors.FirstNameEmptyErrorMessage))
                        result = (string.IsNullOrEmpty(result)) ? result = "First name error message not shown." : result += Environment.NewLine+Environment.NewLine + "First name error message not shown.";
                    //Last name
                    else if (!window.ContainsText(Step1Errors.LastNameEmptyErrorMessage))
                        result = (string.IsNullOrEmpty(result)) ? result = "Last name error message not shown." : result += Environment.NewLine + Environment.NewLine + "Last name error message not shown.";
                    //Email address
                    else if (!window.ContainsText(Step1Errors.EmailEmptyErrorMessage))
                        result = (string.IsNullOrEmpty(result)) ? result = "Email address error message not shown." : result += Environment.NewLine + Environment.NewLine + "Email address error message not shown.";
                    //Password
                    else if (!window.ContainsText(Step1Errors.PasswordEmptyErrorMessage))
                        result = (string.IsNullOrEmpty(result)) ? result = "Password error message not shown." : result += Environment.NewLine + Environment.NewLine + "Password error message not shown.";
                    //CAPTCHA
                    else if (!window.ContainsText(Step1Errors.CaptchaEmptyErrorMessage))
                        result = (string.IsNullOrEmpty(result)) ? result = "CAPTCHA error message not shown." : result += Environment.NewLine + Environment.NewLine + "CAPTCHA error message not shown.";
                    else
                        result += Environment.NewLine+"Empty field validation PASSED";
                }
                else
                    result = (string.IsNullOrEmpty(result)) ? result = "ERROR message not shown" : result += Environment.NewLine + Environment.NewLine + "ERROR message not shown";
            }
            catch (Exception e)
            {
                result = (string.IsNullOrEmpty(result)) ? result = "Empty mandatory field: " + e.ToString() : result += Environment.NewLine + Environment.NewLine + "Empty field: " + e.ToString();
            }

            return result + Environment.NewLine;

            #endregion

        }
        #endregion

        #region Invalid Email Address Format Test (BRSS_B2_RR1_3)
        /* 2010-06-17: FIXED */
        private static string InvalidEmailFormat(Browser window, string URL, DictionaryEntry[] testData)
        {
            #region Local Parameters
            Button saveButton = null;
            string result = string.Empty;
            int fields = 0;
            #endregion

            #region Validating Email Address Format
            try
            {

                window.GoTo(URL);
                window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));
                result = (string.IsNullOrEmpty(result)) ? result = result = Properties.Settings.Default.DATA_SET_DELIM_START + testData[1].Key.ToString() + Properties.Settings.Default.DATA_SET_DELIM_END + Environment.NewLine : result += Environment.NewLine + Properties.Settings.Default.DATA_SET_DELIM_START + testData[1].Key.ToString() + Properties.Settings.Default.DATA_SET_DELIM_END + Environment.NewLine;

                saveButton = window.Button(Find.ByValue(ButtonsName.SaveButton));

                WatiNInputHelper.SetPageInput(window, CommonHelper.GetDataFromDictionary(testData, 1), out fields);
                saveButton.Click();

                //Email address
                if (!window.ContainsText("Invalid email address"))
                    result += Environment.NewLine + "Invalid email address message not shown.";
                else
                    result += "Invalid email address message properly shown.";
            }
            catch (Exception e)
            {
                result = (string.IsNullOrEmpty(result)) ? result = result = Properties.Settings.Default.DATA_SET_DELIM_START + testData[1].Key.ToString() + Properties.Settings.Default.DATA_SET_DELIM_END + e.ToString() : result += result = Properties.Settings.Default.DATA_SET_DELIM_START + testData[1].Key.ToString() + Properties.Settings.Default.DATA_SET_DELIM_END +e.ToString();
            }
            #endregion

            return result + Environment.NewLine;

        }
        #endregion

        #region Email Address Mismatch Test (BRSS_B2_RR1_3)
        /* 2010-06-17: FIXED */
        private static string EmailsMismatch(Browser window, string URL, DictionaryEntry[] testData)
        {
            #region Local Parameters
            Button saveButton = null;
            string result = string.Empty;
            int fields = 0;
            DictionaryEntry thisStepDictionary = testData[2];

            string dataSetHeader = Properties.Settings.Default.DATA_SET_DELIM_START + thisStepDictionary.Key.ToString()+Properties.Settings.Default.DATA_SET_DELIM_END;
            #endregion

            #region Email Address Mismatch
            try
            {
                window.GoTo(URL);
                window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));
                result = (string.IsNullOrEmpty(result)) ? result = dataSetHeader + Environment.NewLine : result += Environment.NewLine + dataSetHeader + Environment.NewLine;

                saveButton = window.Button(Find.ByValue(ButtonsName.SaveButton));

                WatiNInputHelper.SetPageInput(window, CommonHelper.GetDataFromDictionary(testData, 2), out fields);
                saveButton.Click();

                //Email address
                if (!window.ContainsText(Step1Errors.EmailMismatchErrorMessage))
                    result += Environment.NewLine+Environment.NewLine + "Email mismatch message not shown.";
                else
                    result += "Email mismatch message properly shown.";
            }
            catch (Exception e)
            {
                result = (result == null) ? result = "Invalid email address: " + e.ToString() : result += Environment.NewLine+Environment.NewLine + "Invalid email address: " + e.ToString();
            }
            #endregion

            return result + Environment.NewLine;
        }
        #endregion

        #region Password Length Test (BRSS_B2_RR1_3)
        private static string InvalidPasswordLength(Browser window, string URL, DictionaryEntry[] values)
        {
            #region Local Parameters
            Button saveButton = null;
            string result = string.Empty;
            int fields = 0;

            DictionaryEntry thisStepDictionary = values[3];
            string dataSetHeader = Properties.Settings.Default.DATA_SET_DELIM_START + thisStepDictionary.Key.ToString() + Properties.Settings.Default.DATA_SET_DELIM_END;
            #endregion

            #region Validating Password Length
            try
            {
                saveButton = window.Button(Find.ByText("SAVE & CONTINUE"));

                window.GoTo(URL);
                window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));
                result = (string.IsNullOrEmpty(result)) ? result = dataSetHeader + Environment.NewLine : result += Environment.NewLine + dataSetHeader + Environment.NewLine;

                WatiNInputHelper.SetPageInput(window, values, out fields);
                saveButton.Click();

                //Email address
                if (!window.ContainsText("Invalid password length."))
                    result = result += Environment.NewLine+Environment.NewLine + "Invalid password length message not shown.";
                else
                    result = "Invalid password length message properly shown.";
            }
            catch (Exception e)
            {
                result = (string.IsNullOrEmpty(result)) ? result = dataSetHeader + e.ToString() : result += Environment.NewLine + "Empty field: " + e.ToString();
            }
            #endregion

            #region Logging
            result = Environment.NewLine + "[Test Case: BRSS_B2_RR1_3 - INVALID PASSWORD LENGTH]: " + DateTime.Now + Environment.NewLine +
                "-------------------------------------------------------------" + Environment.NewLine + result + Environment.NewLine;


            #endregion

            return result;
        }
        #endregion

        #region Password Mismatch Test (BRSS_B2_RR1_3)
        /* 2010-06-17: FIXED */
        private static string PasswordsMismatch(Browser window, string URL, DictionaryEntry[] testData)
        {
            #region Local Parameters
            Button saveButton = null;
            string result = string.Empty;
            int fields = 0;
            DictionaryEntry thisStepDictionary = testData[4];
            string dataSetHeader = Properties.Settings.Default.DATA_SET_DELIM_START + thisStepDictionary.Key.ToString() + Properties.Settings.Default.DATA_SET_DELIM_END;
            #endregion

            #region Validating Passwords
            try
            {
                window.GoTo(URL);
                window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));
                result = (string.IsNullOrEmpty(result)) ? result = dataSetHeader + Environment.NewLine :
                    result += Environment.NewLine + dataSetHeader + Environment.NewLine;

                saveButton = window.Button(Find.ByValue(ButtonsName.SaveButton));

                WatiNInputHelper.SetPageInput(window, CommonHelper.GetDataFromDictionary(testData, 4), out fields);
                saveButton.Click();

                //Password
                if (!window.ContainsText(Step1Errors.PasswordMismatchErrorMessage))
                    result += Environment.NewLine + "Passwords mismatch message not shown.";
                else
                    result += "Passwords mismatch message properly shown.";
            }
            catch (Exception e)
            {
                result = (string.IsNullOrEmpty(result)) ? result = dataSetHeader + e.ToString() : result += Environment.NewLine + dataSetHeader + e.ToString();
            }
            #endregion

            return result + Environment.NewLine;
        }
        #endregion

        #region Valid Entries Test (BRSS_B2_RR1_3)
        /* 2010-06-17: FIXED */
        private static string ValidEntries(Browser window, string URL, DictionaryEntry[] testData)
        {
            #region Local Parameters
            Button saveButton = null;
            string result = string.Empty;
            int fields = 0;
            DictionaryEntry thisStepDictionary = new DictionaryEntry();
            int thisIndex = 0;
            if (testData.Length > 1)
                thisIndex = 5;
            thisStepDictionary = testData[thisIndex];    
            string dataSetHeader = Properties.Settings.Default.DATA_SET_DELIM_START + thisStepDictionary.Key.ToString() + Properties.Settings.Default.DATA_SET_DELIM_END;
            #endregion

            #region Valid Entries
            try
            {
                window.GoTo(URL);
                window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));
                result = (result == null) ? result = dataSetHeader + Environment.NewLine :
                    result += Environment.NewLine + dataSetHeader + Environment.NewLine;

                saveButton = window.Button(Find.ByValue("SAVE & CONTINUE").And(Find.ByName("Submit")));

                WatiNInputHelper.SetPageInput(window, CommonHelper.GetDataFromDictionary(testData, thisIndex), out fields);
                saveButton.Click();
                window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT) * 2);

                //Step 2 appears
                if (!window.ContainsText(StepHeaderText.Step2HeaderMessage))
                    result += Environment.NewLine + "Step 1 not completed successfully.";
                else
                    result += "Step 1 registration completed successfully.";
            }
            catch (Exception e)
            {
                result = (string.IsNullOrEmpty(result)) ? result = dataSetHeader + e.ToString() : result += Environment.NewLine + dataSetHeader + e.ToString();
            }
            #endregion

            return result + Environment.NewLine;
        }
        #endregion
        #endregion

        #region 1st Step Save & Continue Action
        public static string Save1stStep(Browser window, DictionaryEntry[] tempTestData)
        {
            int noOfFields = 0;
            Button submit;
            string result = string.Empty;

            window.GoTo(Properties.Settings.Default.NEW_RESTAURANT_REGISTRATION_URL);
            window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));

            ArrayList tempList = CommonHelper.GetDataFromDictionary(tempTestData, 0);
            WatiNInputHelper.SetPageInput(window, tempList, out noOfFields);

            submit = window.Button(Find.ByValue(ButtonsName.SaveButton));
            submit.Click();
            window.WaitForComplete(int.Parse(Properties.Settings.Default.TIMEOUT));

            if (window.ContainsText(StepHeaderText.Step2HeaderMessage))
                result += Environment.NewLine+"Save & Continue button for completed form works properly." + Environment.NewLine;
            else
                result += Environment.NewLine+"Save & Continue button for completed form does not workS properly." + Environment.NewLine;

            return result;
        }
        #endregion

        #endregion
    }
}
